# test02_thread.py
# 通过 ping 测试主机是否可达
# 如果 ping 不通，不管什么原因都认为主机不可用
# 通过多线程方式实现并发扫描
import subprocess
import threading
def ping(host):
    result = subprocess.run("ping -c 2 %s &> /dev/null" % host, shell=True)
    if result.returncode == 0:  # returncode值为0，表示指令执行成功
        print("%s: up" % host)
    else:
        print("%s: down" % host)
if __name__ == '__main__':
    address = [ "www.baidu.com", "www.163.com", "www.qq.com",
        "www.baidu.com", "www.163.com", "www.qq.com",
        "www.baidu.com", "www.163.com", "www.qq.com"]
    # 多线程(多执行阻塞任务)
    for item in address:
        t = threading.Thread(target=ping, args=(item, ))
        t.start()
    # 单线程: 一个一个地址ping, 速度慢
    # for item in address:
    #     ping(item)




